package com.aaa.sso.service;


import com.aaa.entity.BBusinessInfo;
import com.aaa.entity.EEmpInfo;
import com.aaa.entity.UUserInfo;
import com.aaa.sso.feign.UserService;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class MyCustomUserDetailsService implements CustomUserDetailService{
    @Resource
    private UserService userService;

//    全局变量

    @Override
    public UserDetails loadUserByUsernameAndType(String var1, String var2) throws UsernameNotFoundException {
        if(var2.equals("u-user-info_PHONE")){
            // 查询 emp表   123    123456
           return loadUserByUsername1(var1);
        }else if(var2.equals("b-business-info_PHONE")){
            return loadUserByUsername4(var1);
        } else if(var2.equals("EMP_PHONE")){
            return loadUserByUsername2(var1);
        }
        return null;
    }
    private UserDetails loadUserByUsername2(String var1) {
        EEmpInfo emp = userService.getByUserName(var1);
        if (emp == null){
            throw new UsernameNotFoundException("用户名不对");
        }
        List<String> byUid = userService.findByUid(emp.getId());
        List<SimpleGrantedAuthority> collect = byUid.stream().map(s -> new SimpleGrantedAuthority(s)).collect(Collectors.toList());
        return new User(var1, emp.getPassword(), collect);
    }

    private UserDetails loadUserByUsername1(String var1) {
        UUserInfo one=userService.getByPhone(var1);
        System.out.println("数据库中的密码="+one.getPassword());
        System.out.println("username"+one.getUname());
        if(one==null){
            System.out.println("用户名不对");
            throw new UsernameNotFoundException("用户名不对");
        }
        //BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        return new User(var1,one.getPassword(), AuthorityUtils.commaSeparatedStringToAuthorityList("ROLE_user"));
    }
    private UserDetails loadUserByUsername4(String var1) {
        BBusinessInfo three=userService.getBusinessByPhone(var1);
        System.out.println("数据库中的密码="+three.getPassword());
        System.out.println("telephone="+three.getTelephone());
        if(three==null){
            System.out.println("用户名不对");
            throw new UsernameNotFoundException("用户名不对");
        }
//        这里你的用户登录也同样要在最后改一下，改成自己的logintype
        return new User(var1, three.getPassword(), AuthorityUtils.commaSeparatedStringToAuthorityList("b-business-info_PHONE"));
    }



    @Override
    public UserDetails loadUserByUsername(String s) throws UsernameNotFoundException {
        return null;
    }
}
